package com.fmy.jurisdiction.modular.controller;


import com.fmy.jurisdiction.core.commons.kits.SecurityKit;
import com.fmy.jurisdiction.core.web.call.ResponseCall;
import com.fmy.jurisdiction.core.web.specs.Response;
import com.fmy.jurisdiction.core.web.util.WebUtil;
import com.fmy.jurisdiction.modular.model.AdminRole;
import com.fmy.jurisdiction.modular.service.IAdminRoleService;
import com.fmy.jurisdiction.modular.wrapper.obj.AdminRoleWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Mr.fmy
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/admin-role")
public class AdminRoleController {

    @Autowired
    private IAdminRoleService roleService;

    @PostMapping("/add")
    @Valid
    public Response addRole(AdminRole role, BindingResult result) {
        WebUtil.validationParam(result);
        role.setCreateUser(SecurityKit.getId());
        return WebUtil.vailBusiness(roleService.addRole(role), "新增角色失败、请稍后重试");
    }

    @PostMapping("/modify-role")
    public Response modifyRole(Integer rid, String roleName) {
        return WebUtil.vailBusiness(roleService.modifyRole(rid, roleName, SecurityKit.getId()), "修改角色失败、请稍后重试");
    }

    @PostMapping("/del-role")
    public Response delRole(Integer rid) {
        return WebUtil.vailBusiness(roleService.delRole(rid), "删除角色失败、请稍后重试");
    }

    @PostMapping("/allocation-prem")
    public Response allocationPrem(Integer rid, Integer premId, boolean type) {
        return WebUtil.vailBusiness(roleService.allocationPrem(rid, premId, type), "分配权限失败、请稍后重试");
    }

    @GetMapping("/get-tree-list")
    public Response getTreeList() {
        return ResponseCall.success(new AdminRoleWrapper(roleService.getRoleChildren(null)).wrapper());
    }

}

